﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using DAL;

namespace UI.Admin
{
    public partial class sanpham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HienThiDanhSach();
        }
        void HienThiDanhSach()
        {
            // Get All Sản Phẩm
            DataTable Manufacturers = DBConnection.getALL("sp_getAllSanPham");
            GridView1.DataSource = Manufacturers;
            GridView1.DataBind();

            MultiView1.ActiveViewIndex = 0;
            btninsert.Enabled = false;
        }


        void HienThiKetQuaSua(String manxb)
        {

            SqlParameter[] thamso = new SqlParameter[]
            {
            new SqlParameter("@MaSP",manxb)
            };
            SqlParameter[] thamso1 = new SqlParameter[]
            {
            new SqlParameter("@MaSP",manxb)
            };
            DataTable dtsp = DBConnection.GetDatatableByStoreProcedureName("sp_getSanPhambyMaSP", thamso);

            DataTable dtctsp = DBConnection.GetDatatableByStoreProcedureName("sp_ChiTietSanPhamByMaSP", thamso1);
            
            DataTable dtNhasx = DBConnection.getALL("sp_getAllNhaSX");
            txtMaNSX.DataSource = dtNhasx;
            txtMaNSX.DataValueField = "MaNSX";
            txtMaNSX.DataTextField = "TenNSX";
            txtMaNSX.DataBind();
            lbmasp.Text = dtsp.Rows[0]["MaSP"].ToString();
            txttensp.Text = dtsp.Rows[0]["TenSP"].ToString();
            txtMaNSX.Text = dtsp.Rows[0]["MaNSX"].ToString();
            txtdongia.Text = dtsp.Rows[0]["DonGia"].ToString();
            txtsoluong.Text = dtsp.Rows[0]["SoLuong"].ToString();
            txtngaysx.Text = dtsp.Rows[0]["NgaySX"].ToString();
            txtngaycapnhat.Text = dtsp.Rows[0]["NgayCapNhat"].ToString();
            Image2.ImageUrl = "~/images/" + dtsp.Rows[0]["Image1"].ToString();


            txtHDH.Text = dtctsp.Rows[0]["HeDieuHanh"].ToString();
            txtManHinh.Text = dtctsp.Rows[0]["ManHinh"].ToString();
            txtDLPin.Text = dtctsp.Rows[0]["DungLuongPin"].ToString();
            txtcamerasau.Text = dtctsp.Rows[0]["CameraSau"].ToString();
            txtcameratruoc.Text = dtctsp.Rows[0]["CameraTruoc"].ToString();
            txtcpu.Text = dtctsp.Rows[0]["CPU"].ToString();
            txtRam.Text = dtctsp.Rows[0]["Ram"].ToString();
            txtRom.Text = dtctsp.Rows[0]["Rom"].ToString();

            MultiView1.ActiveViewIndex = 1;
        }


        protected void btntaomoi_Click(object sender, EventArgs e)
        {
            DataTable dtNhasx = DBConnection.getALL("sp_getAllNhaSX");
            txtMaNSX.DataSource = dtNhasx;
            txtMaNSX.DataValueField = "MaNSX";
            txtMaNSX.DataTextField = "TenNSX";
            txtMaNSX.DataBind();
            btnupdate.Visible = false;
            btninsert.Visible = true;
            MultiView1.ActiveViewIndex = 1;
            btninsert.Enabled = true;
            lbmasp.Text = "Mã tự động tăng";

        }


        protected void btntmkiem_Click(object sender, EventArgs e)
        {

            //string tennxb = txttim.Text;
            //SqlParameter[] thamso = new SqlParameter[]{
            //new SqlParameter("p_tennhaxb",tennxb)
            //};

            //// search NhaXB by TenNhaXB
            //DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_SearchNhaXuatBanByTenNhaXB", thamso);
            //GridView1.DataSource = dt;
            //GridView1.DataBind();
            //txttim.Text = "";
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.HienThiDanhSach();

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;

            string masp = GridView1.SelectedRow.Cells[2].Text;
            this.HienThiKetQuaSua(masp);
        }
        protected void btnback_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }


        protected void btnupdate_Click(object sender, EventArgs e)
        {
            string maSP = lbmasp.Text;
            string tensp = txttensp.Text;
            SqlParameter[] thamso = new SqlParameter[] {
            new SqlParameter("@MaSP", maSP),
            };

            // get sampham by MaSP
            DataTable dt = DBConnection.GetDatatableByStoreProcedureName("sp_getSanPhambyMaSP", thamso);
            string hinh;
            if (FileUpload2.HasFile)
            {
                string strfile = "~/images/" + FileUpload2.FileName;
                string s = Server.MapPath(strfile);
                FileUpload2.SaveAs(s);
                hinh = FileUpload2.FileName;
            }
            else
                hinh = dt.Rows[0]["Image1"].ToString(); 




            SqlParameter[] thamso1 = new SqlParameter[]
            {
            new SqlParameter("@MaSP",int.Parse(lbmasp.Text)),
            new SqlParameter("@MaNSX",int.Parse(txtMaNSX.Text)),
            new SqlParameter("@TenSP",txttensp.Text),
            new SqlParameter("@Image1",hinh),
            new SqlParameter("@DonGia",decimal.Parse(txtdongia.Text)),
            new SqlParameter("@SoLuong",int.Parse(txtsoluong.Text)),
            new SqlParameter("@NgaySX",DateTime.Parse(txtngaysx.Text)),
            new SqlParameter("@NgayCapNhat",DateTime.Parse(txtngaycapnhat.Text)),
             };

            SqlParameter[] thamso2 = new SqlParameter[]
            {
                new SqlParameter("@MaSP",maSP),
                new SqlParameter("@HeDieuHanh",txtHDH.Text),    
                new SqlParameter("@ManHinh",txtManHinh.Text),
                new SqlParameter("@CameraSau",txtcamerasau.Text),
                new SqlParameter("@CameraTruoc",txtcameratruoc.Text),
                new SqlParameter("@CPU",txtcpu.Text),
                new SqlParameter("@Ram",txtRam.Text),
                new SqlParameter("@Rom",txtRom.Text), 
                new SqlParameter("@DungLuongPin",txtDLPin.Text) 
             };
            // update information of NhaXB by MaNXB
            DBConnection.QueryByStoreProcedureName("sp_updateSanPham", thamso1);
            DBConnection.QueryByStoreProcedureName("sp_updateChiTietSanPham", thamso2);
            this.HienThiDanhSach();
        }


        protected void btninsert_Click(object sender, EventArgs e)
        {

            //tao ma nha xuat ban tu dong tang = lay max manhaxb+1 
            DataTable dt= DBConnection.getALL("sp_countSanPham");
            int maSP = int.Parse(dt.Rows[0]["soluong"].ToString()) + 2;

            string tensp = txttensp.Text;
            string hinh = FileUpload2.FileName;
            SqlParameter[] thamso = new SqlParameter[]
            {
            new SqlParameter("@MaSP",maSP),
            new SqlParameter("@MaNSX",int.Parse(txtMaNSX.Text)),    
            new SqlParameter("@TenSP",tensp),
            new SqlParameter("@Image1",hinh),
            new SqlParameter("@DonGia",decimal.Parse(txtdongia.Text)),
            new SqlParameter("@SoLuong",int.Parse(txtsoluong.Text))  ,
            new SqlParameter("@NgaySX",DateTime.Parse(txtngaysx.Text)),
            new SqlParameter("@NgayCapNhat",DateTime.Parse(txtngaycapnhat.Text)) 
             };
             
            SqlParameter[] thamso1 = new SqlParameter[]
            {
            new SqlParameter("@MaSP",maSP),
            new SqlParameter("@HeDieuHanh",txtHDH.Text),    
            new SqlParameter("@ManHinh",txtManHinh.Text),
            new SqlParameter("@CameraSau",txtcamerasau.Text),
            new SqlParameter("@CameraTruoc",txtcameratruoc.Text),
            new SqlParameter("@CPU",txtcpu.Text),
            new SqlParameter("@Ram",txtRam.Text),
            new SqlParameter("@Rom",txtRom.Text), 
            new SqlParameter("@DungLuongPin",txtDLPin.Text) 
             };

            DBConnection.QueryByStoreProcedureName("sp_insertSanPham", thamso);
            DBConnection.QueryByStoreProcedureName("sp_insertChiTietSanPham", thamso1);
            this.HienThiDanhSach();
        }


        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            string maSP = GridView1.Rows[e.RowIndex].Cells[2].Text;

            try
            {
                SqlParameter[] thamso = new SqlParameter[]
                 {
                new SqlParameter("@MaSP",int.Parse(maSP)),
                 };
                SqlParameter[] thamso1 = new SqlParameter[]
                 {
                new SqlParameter("@MaSP",int.Parse(maSP)),
                 };
                DBConnection.QueryByStoreProcedureName("sp_deleteChiTietSanPhambyMaSP", thamso1);
                DBConnection.QueryByStoreProcedureName("sp_deleteSanPhambyMaSP",thamso);
            }
            catch
            {
                System.Console.WriteLine("khong xoa duoc do rang buoc khoa");
            }
            HienThiDanhSach();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }
    }

}